﻿using System.Linq;
using Dyn.News.Services;
using Orchard.Localization;
using Orchard.UI.Navigation;

namespace Dyn.News {
    public class AdminMenu : INavigationProvider {
        private readonly ICategoryervice _Categoryervice;

        public AdminMenu(ICategoryervice Categoryervice) {
            _Categoryervice = Categoryervice;
        }

        public Localizer T { get; set; }

        public string MenuName { get { return "admin"; } }

        public void GetNavigation(NavigationBuilder builder) {
            builder.AddImageSet("category")
                .Add(T("Category"), "1.5", BuildMenu);
        }

        private void BuildMenu(NavigationItemBuilder menu) {
            var Category = _Categoryervice.Get();
            var CategoryCount = Category.Count();
            var singleCategory = CategoryCount == 1 ? Category.ElementAt(0) : null;

            if (CategoryCount > 0 && singleCategory == null) {
                menu.Add(T("Manage Categories"), "3",
                         item => item.Action("List", "CategoryAdmin", new { area = "Dyn.News" }).Permission(Permissions.MetaListCategory));
            }
            else if (singleCategory != null)
                menu.Add(T("Manage Category"), "1.0",
                    item => item.Action("Item", "CategoryAdmin", new { area = "Dyn.News", categoryID = singleCategory.Id }).Permission(Permissions.MetaListOwnCategory));

            if (singleCategory != null)
                menu.Add(T("New Post"), "1.1",
                         item =>
                         item.Action("Create", "ArticleAdmin", new { area = "Dyn.News", categoryID = singleCategory.Id }).Permission(Permissions.MetaListOwnCategory));

            menu.Add(T("New Category"), "1.2",
                     item =>
                     item.Action("Create", "CategoryAdmin", new { area = "Dyn.News" }).Permission(Permissions.ManageCategory));

        }
    }
}